﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Framework.Extensions
{
    public static class BlockingCollectionExtensions
    {
        public static void AddAndFinallyComplete<T>(this BlockingCollection<T> source, Func<IEnumerable<T>> generator)
        {
            try
            {
                generator().Foreach(source.Add);
            }
            finally { source.CompleteAdding(); }

        }

        public static void ExecuteAndFinallyComplete<T>(this BlockingCollection<T> source, Action action)
        {
            Task.Factory.StartNew(() =>
            {
                try { action(); }
                finally { source.CompleteAdding(); }
            });

        }
    }
}